if ("`swdLocal'\Data"=="") {
	di as error "Please run init.do first."
	error 1
}
clear all
set more off
set maxvar 10000

*Open file for regressions
use "${gsdAnalysisOutput}/bscie_panel_indices_final", clear

*Keep only endline and lagged controls
keep if wave==2 & final_sample==1

ren edu_level_b  edu_b

*Define strata
egen strata=group(gender state_old)

*Generate treatment streams
gen treatmentX=0
replace treatmentX=1 if treatment_stream==3


*Generate IV treatment x kcb_dist 
gen Z_treat_kcb=treatment*lg_kcb_dist
label var Z_treat_kcb "Treatment x (log) distance to KCB branch"

*Generate interaction for other geographic controls
gen treat_citydist=treatment*lg_city_dist
label var treat_citydist "Treatment x (log) distance to city center"
gen treat_roaddist=treatment*lg_road_dist
label var treat_roaddist "Treatment x (log) distance to primary road"
gen treat_gradient=treatment*gradient
label var treat_gradient "Treatment x gradient"
gen treat_alt=treatment*info_loc_gps_altitude_old
label var treat_alt "Treatment x altitude"
gen treat_conflict = treatment*death_x_prox_a300
label var treat_conflict "Treatment x conflict_affected_index(300km buffer)"
gen treat_bank = treatment*lg_bank_dist
label var treat_bank "Treatment x distance to any bank"
gen treat_edu = treatment*edu_b
gen treat_num = treatment*num_eval_b
gen treat_lit = treatment*lit_eval_b


* Treatment conflict interactions
gen treat_UCDP_fatalities = death_x_prox_a300*treatment
gen treat_UCDP_events = conflictevents_x_prox_b300*treatment
gen treat_ACLED_fatalities = ACLEDfatalities_x_proxb300*treatment
gen treat_ACLED_events = ACLEDevents_x_proxb300*treatment

*Define geographic controls
global geo_controls lg_city_dist treat_citydist treat_roaddist lg_road_dist gradient treat_gradient 


***Socioeconomic Survey outcomes ******************************************************************
clear matrix
mat A = J(9,24,.)
mat C = J(1,12,.)
mat D = J(1,12,.)


global n 1
local replace replace
local append
foreach var of varlist employment_index consumption_index savings_debt_index bizskills_index ///
psycho_index risk_index trust_index crime_index migration_index {


	*ATE - 2 instruments
	eststo: xi: ivreg2 `var' lg_kcb_dist treatment $geo_controls  treat_UCDP_fatalities death_x_prox_a300 i.strata (treatmentX= Z_treat_kcb) , cluster(boma_old) first savefirst savefprefix(fstage_) 
		local b_t1 = _b[treatmentX]
		display `b_t1'
		mat A[$n,1] = `b_t1'
		local p_t1=(2 * ttail(e(df_m), abs(_b[treatmentX]/_se[treatmentX])))
		display `p_t1'
		mat A[$n,2] = `p_t1'
		local b_t2 = _b[treatment]
		display `b_t2'
		mat A[$n, 3] = `b_t2'
		local p_t2=(2 * ttail(e(df_m), abs(_b[treatment]/_se[treatment])))
		display `p_t2'
		mat A[$n, 4] = `p_t2'
		
		lincom treatmentX + treatment
		local b_t1 = `r(estimate)'
		display `b_t1'
		mat A[$n,5] = `b_t1'
		local p_t1= `r(p)'
		display `p_t1'
		mat A[$n,6] = `p_t1'
		
		local obs = `e(N)'
		display `obs'
		mat C[1,1] = `obs'
		local fstat = `e(rkf)'
		display `fstat'
		mat D[1,1] = `fstat'
		
		
		eststo: xi: ivreg2 `var' lg_kcb_dist treatment $geo_controls  treat_UCDP_events conflictevents_x_prox_b300 i.strata (treatmentX= Z_treat_kcb) , cluster(boma_old) first savefirst savefprefix(fstage_) 
		local b_t1 = _b[treatmentX]
		display `b_t1'
		mat A[$n, 7] = `b_t1'
		local p_t1=(2 * ttail(e(df_m), abs(_b[treatmentX]/_se[treatmentX])))
		display `p_t1'
		mat A[$n, 8] = `p_t1'
		local b_t2 = _b[treatment]
		display `b_t2'
		mat A[$n, 9] = `b_t2'
		local p_t2=(2 * ttail(e(df_m), abs(_b[treatment]/_se[treatment])))
		display `p_t2'
		mat A[$n, 10] = `p_t2'
		local obs = `e(N)'
		mat C[1,4] = `obs'
		local fstat = `e(rkf)'
		mat D[1,4] = `fstat'
		
		lincom treatmentX + treatment
		local b_t1 = `r(estimate)'
		display `b_t1'
		mat A[$n,11] = `b_t1'
		local p_t1= `r(p)'
		display `p_t1'
		mat A[$n,12] = `p_t1'
		
			eststo: xi: ivreg2 `var' lg_kcb_dist treatment $geo_controls  treat_ACLED_fatalities ACLEDfatalities_x_proxb300 i.strata (treatmentX= Z_treat_kcb) , cluster(boma_old) first savefirst savefprefix(fstage_) 
		local b_t1 = _b[treatmentX]
		display `b_t1'
		mat A[$n, 13] = `b_t1'
		local p_t1=(2 * ttail(e(df_m), abs(_b[treatmentX]/_se[treatmentX])))
		display `p_t1'
		mat A[$n, 14] = `p_t1'
		local b_t2 = _b[treatment]
		display `b_t2'
		mat A[$n, 15] = `b_t2'
		local p_t2=(2 * ttail(e(df_m), abs(_b[treatment]/_se[treatment])))
		display `p_t2'
		mat A[$n, 16] = `p_t2'
		local obs = `e(N)'
		mat C[1,7] = `obs'
		local fstat = `e(rkf)'
		mat D[1,7] = `fstat'
		
		lincom treatmentX + treatment
		local b_t1 = `r(estimate)'
		display `b_t1'
		mat A[$n,17] = `b_t1'
		local p_t1= `r(p)'
		display `p_t1'
		mat A[$n,18] = `p_t1'
		
			eststo: xi: ivreg2 `var' lg_kcb_dist treatment $geo_controls  treat_ACLED_events ACLEDevents_x_proxb300 i.strata (treatmentX= Z_treat_kcb) , cluster(boma_old) first savefirst savefprefix(fstage_) 
		local b_t1 = _b[treatmentX]
		display `b_t1'
		mat A[$n, 19] = `b_t1'
		local p_t1=(2 * ttail(e(df_m), abs(_b[treatmentX]/_se[treatmentX])))
		display `p_t1'
		mat A[$n, 20] = `p_t1'
		local b_t2 = _b[treatment]
		display `b_t2'
		mat A[$n, 21] = `b_t2'
		local p_t2=(2 * ttail(e(df_m), abs(_b[treatment]/_se[treatment])))
		display `p_t2'
		mat A[$n, 22] = `p_t2'
		local obs = `e(N)'
		mat C[1,10] = `obs'
		local fstat = `e(rkf)'
		mat D[1,10] = `fstat'
		
		lincom treatmentX + treatment
		local b_t1 = `r(estimate)'
		display `b_t1'
		mat A[$n,23] = `b_t1'
		local p_t1= `r(p)'
		display `p_t1'
		mat A[$n,24] = `p_t1'
		
		
	local replace
	local append append
	global n=$n+1
}

		matrix stars = J(9,24,0)
		foreach k of numlist 1/9 {
		matrix stars[`k',1] = (abs(A[`k',2]) < 0.1) + (abs(A[`k',2]) < 0.05) + (abs(A[`k',2]) < 0.01)
		matrix stars[`k',3] = (abs(A[`k',4]) < 0.1) + (abs(A[`k',4]) < 0.05) + (abs(A[`k',4]) < 0.01)
		matrix stars[`k',5] = (abs(A[`k',6]) < 0.1) + (abs(A[`k',6]) < 0.05) + (abs(A[`k',6]) < 0.01)
		matrix stars[`k',7] = (abs(A[`k',8]) < 0.1) + (abs(A[`k',8]) < 0.05) + (abs(A[`k',8]) < 0.01)
		matrix stars[`k',9] = (abs(A[`k',10]) < 0.1) + (abs(A[`k',10]) < 0.05) + (abs(A[`k',10]) < 0.01)
		matrix stars[`k',11] = (abs(A[`k',12]) < 0.1) + (abs(A[`k',12]) < 0.05) + (abs(A[`k',12]) < 0.01)
		matrix stars[`k',13] = (abs(A[`k',14]) < 0.1) + (abs(A[`k',14]) < 0.05) + (abs(A[`k',14]) < 0.01)
		matrix stars[`k',15] = (abs(A[`k',16]) < 0.1) + (abs(A[`k',16]) < 0.05) + (abs(A[`k',16]) < 0.01)
				matrix stars[`k',17] = (abs(A[`k',18]) < 0.1) + (abs(A[`k',18]) < 0.05) + (abs(A[`k',18]) < 0.01)
		matrix stars[`k',19] = (abs(A[`k',20]) < 0.1) + (abs(A[`k',20]) < 0.05) + (abs(A[`k',20]) < 0.01)
		matrix stars[`k',21] = (abs(A[`k',22]) < 0.1) + (abs(A[`k',22]) < 0.05) + (abs(A[`k',22]) < 0.01)
		matrix stars[`k',23] = (abs(A[`k',24]) < 0.1) + (abs(A[`k',24]) < 0.05) + (abs(A[`k',24]) < 0.01)
		  }

		matrix rownames A = "Employment index" "Consumption index" "Savings index" "Business skills index" ///
		"Psychological wellbeing index" "Risk index" "Trust index" "Crime and violence index" "Migration index" 
		frmttable , statmat(A) replace sdec(3)  substat(1) plain annotate(stars) asymbol(*,**,***)  /// 
		title (Table A.10 – Robustness of LATE results from Table 4 to different conflict measures) 
		frmttable , statmat(C) replace sdec(0) append rtitles("Observations")
		frmttable using "$gsdTables/TableA.10.tex", statmat(D) replay replace tex sdec(3) append rtitles("F-stat") plain


